package com.hp.attendance.serlvet.empmanage.zaizhi;

import com.hp.attendance.entity.Employee;
import com.hp.attendance.entity.History;
import com.hp.attendance.service.EmployeeService;
import com.hp.attendance.service.HistoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

@WebServlet("/historyZZAdd")
public class EmpManageZZHistoryAddServlet extends HttpServlet {
    private HistoryService historyService = new HistoryService();
    private EmployeeService employeeService = new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int employee_number = Integer.parseInt(req.getParameter("employee_number"));
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String gender = req.getParameter("gender");
            String brithday = req.getParameter("date");
            Date birthday1 = null;
            try {
                birthday1 = new SimpleDateFormat("yyyy-MM-dd").parse(brithday);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String telephone = req.getParameter("telephone");
            String email = req.getParameter("email");
            String address = req.getParameter("address");
            String education = req.getParameter("education");
            int department_number = Integer.parseInt(req.getParameter("departmentNumber"));
            int position_number = Integer.parseInt(req.getParameter("positionNumber"));
            String notes = req.getParameter("notes");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            Date in_time =null;
            String status = "在职";
            try {
                in_time = sdf.parse(sdf.format(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Employee employee = new Employee(employee_number,name, password, gender, birthday1, telephone, email, address, education, department_number, position_number,in_time, notes);
            employeeService.empAdd(employee);
            History history = new History(employee_number, name, gender, birthday1, telephone, email, address, education, in_time, department_number, position_number, status, notes);
            historyService.hisAdd(history);
            req.getRequestDispatcher("/empMangeZZList").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
